/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 Pentaho Corporation.  All rights reserved.
 *
 * @author Will Gorman (wgorman@pentaho.com)
 */
package org.pentaho.actionsequence.dom.actions;

//import java.lang.reflect.Constructor;
//import java.lang.reflect.Method;

import org.dom4j.Element;
import org.pentaho.actionsequence.dom.actions.SqlQueryAction;

public class ActionFactory {
  public static final Class[] AVAILABLE_ACTIONS = new Class[] {
    JFreeReportAction.class, // DONE
    JasperReportAction.class, // DONE
    BirtReportAction.class, //  DONE
    BarChartAction.class,
    PieChartAction.class,
    LineChartAction.class,
    AreaChartAction.class,
    SqlQueryAction.class, // DONE
    SqlDataAction.class,
    SqlExecuteAction.class,
    MdxQueryAction.class,
    HQLQueryAction.class,
    XQueryAction.class,
    XQueryConnectionAction.class,
    PivotViewAction.class, // DONE
    ResultSetCompareAction.class,
    ListSchedJobsAction.class,
    ResumeSchedulerAction.class,
    SchedulerStatusAction.class,
    SuspendSchedulerAction.class,
    SuspendScheduledJobAction.class,
    ResumeScheduledJobAction.class,
    StartScheduledJobAction.class,
    DeleteScheduledJobAction.class,
    KettleTransformAction.class,
    KettleJobAction.class,
    FormatMsgAction.class,
    CopyParamAction.class,
    ContentOutputAction.class,
    PrintParamAction.class,
    PrintMapValsAction.class,
    HelloWorldAction.class,
    MQLAction.class,
    JMSAction.class,
    JFreeReportGenAction.class,
    EmailAction.class,
    JavascriptAction.class, // DONE
    PrinterAction.class,
    SharkWorkflowAction.class,
    ReceiptAuditAction.class,
    TemplateMsgAction.class,
    SecureFilterAction.class, // DONE
    SubActionAction.class,
    SqlConnectionAction.class,
    MdxConnectionAction.class,
    HQLConnectionAction.class
  };
  
  public static Object newInstance(Class clazz) {
    if (clazz.equals(SqlQueryAction.class)) {
      return new SqlQueryAction();
    } else if (clazz.equals(JFreeReportAction.class)) {
      return new JFreeReportAction();
    } else if (clazz.equals(SecureFilterAction.class)) {
      return new SecureFilterAction();
    } else if (clazz.equals(PivotViewAction.class)) {
      return new PivotViewAction();
    } else if (clazz.equals(JavascriptAction.class)) {
      return new JavascriptAction();
    } else if (clazz.equals(JasperReportAction.class)) {
      return new JasperReportAction();
    } else if (clazz.equals(BirtReportAction.class)) {
      return new BirtReportAction();
    } else {
      System.out.println("Unrecognized : " + clazz);
      return null;
    }
  }
  public static ActionDefinition getActionDefinition(Element actionDefDomElement, IActionParameterMgr actionInputProvider) {
    ActionDefinition actionDefinition = null;
    for (int i = 0; (i < AVAILABLE_ACTIONS.length) && (actionDefinition == null); i++) {
      // TODO: Will need to replace with code generation
      if (SqlQueryAction.accepts(actionDefDomElement)) {
        actionDefinition = new SqlQueryAction(actionDefDomElement, actionInputProvider);
      } else if (JFreeReportAction.accepts(actionDefDomElement)) {
        actionDefinition = new JFreeReportAction(actionDefDomElement, actionInputProvider);
      } else if (SecureFilterAction.accepts(actionDefDomElement)) {
        actionDefinition = new SecureFilterAction(actionDefDomElement, actionInputProvider);
      } else if (PivotViewAction.accepts(actionDefDomElement)) {
        actionDefinition = new PivotViewAction(actionDefDomElement, actionInputProvider);
      } else if (JavascriptAction.accepts(actionDefDomElement)) {
        actionDefinition = new JavascriptAction(actionDefDomElement, actionInputProvider);
      } else if (JasperReportAction.accepts(actionDefDomElement)) {
        actionDefinition = new JasperReportAction(actionDefDomElement, actionInputProvider);
      } else if (BirtReportAction.accepts(actionDefDomElement)) {
        actionDefinition = new BirtReportAction(actionDefDomElement, actionInputProvider);
      }
//      try {
//        Method acceptElementMethod = AVAILABLE_ACTIONS[i].getMethod("accepts", new Class[]{Element.class});
//        if (Boolean.TRUE.equals(acceptElementMethod.invoke(null, new Object[]{actionDefDomElement}))) {
//          Constructor constructor = AVAILABLE_ACTIONS[i].getConstructor(new Class[]{Element.class, IActionParameterMgr.class});
//          actionDefinition = (ActionDefinition)constructor.newInstance(new Object[]{actionDefDomElement, actionInputProvider});
//        }
//      } catch (Exception e) {
//        e.printStackTrace();
//      }
    }
    if (actionDefinition == null) {
      actionDefinition = new ActionDefinition(actionDefDomElement, actionInputProvider);
    }
    return actionDefinition;
  }

}
